<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// |  Author: Dreamy <294207330@qq.com> 
// +----------------------------------------------------------------------

namespace Home\Controller; 
use Common\Model\TradeModel; 
/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 * $url= $_SERVER[HTTP_HOST]; //获取当前域名  
 * 主要负责APP中一部分页面的WAP端展示
 */
class IndexController extends HomeController
{
    /**
     * 检测登录 WX_OAUTH认证
     */
    public function _initialize(){
        // 检测登录
        $this->check_login(); 
        $this->assign('userInfo',$this->user);  
        $this->assign('navi',1);
    }
    

    /**
     * 前台首页*
     * @return [type] [description]
     */
    public function index(){  
        redirect(U('articles')); 
    } 

    /**
     * 获取文章列表返回 
     * @return [type] [description]
     */
    public function arctilelists($page=1){
        $this->pageNum  = 20 ;
        $listsort       = "sort ASC,create_time DESC" ;  //排序
        $map['status']  = 1 ;
        $map['display'] = 1 ;  
        $list       = M('article')->where($map)->field("*")->order( $listsort )->page($page,$this->pageNum)->select();
        $this->ajaxReturn(array('status' => 1,'info'=>'获取成功','data'=>$list), 'json') ;  
    }

    /**
     * 动态列表
     * @return [type] [description]
     */
    public function articles(){
        $this->meta_title = '项目动态' ; 
        $this->display() ; 
    }

    /**
     * 报备列表
     * @return [type] [description]
     */
    public function baobei(){
        $this->meta_title = '报备记录' ; 
        $this->assign('navi',2); 
        $this->display() ; 
    }

    /**
     * 佣金记录
     * @return [type] [description]
     */
    public function yongjin(){
        $this->meta_title = '佣金明细' ;
        $this->assign('navi',3);  
        $this->display() ; 
    }  
    
    /**
     * 获取报备记录返回 
     * @return [type] [description]
     */
    public function baobeilists($page=1){ 
        $this->pageNum  = 20 ;
        $map['uid']     = $this->uid ;
        if( I('status') == 1 ){
            $map['status'] = array('egt',1) ;     
        }
        $listsort       = "create_time DESC" ;
        $list           = M('baobei')->where($map)->field("*")->order( $listsort )->page($page,$this->pageNum)->select();
        $this->ajaxReturn(array('status' => 1,'info'=>'获取成功','data'=>$list), 'json') ;  
    }

     /**
     * 新增报备功能
     * @return [type] [description]
     */ 
    public function baobeiadd(){
        if(IS_GET){
            $projectList = M('project')->order('sort ASC')->select();
            $this->assign('projectList', $projectList); 
            $this->meta_title = '新增报备' ; 
            $this->display() ; 
        }else{
            $data = I('post.');
            if(!$data['project_id']){
                echo json_encode(array('status'=>0,'info'=>'请选择项目')) ; 
                exit() ;
            }
            if(!$data['name']){
                echo json_encode(array('status'=>0,'info'=>'请填写客户名称')) ; 
                exit() ;
            }
            if(!$data['tel']){
                echo json_encode(array('status'=>0,'info'=>'请填写客户电话')) ; 
                exit() ;
            }
            $pinfo = M('project')->where(array('id'=>$data['project_id']))->find();
            if(!$pinfo){
                echo json_encode(array('status'=>0,'info'=>'项目不存在')) ; 
                exit() ;
            }
            $data['uid'] = $this->uid ; 
            $data['project_title'] = $pinfo['title'] ; 
            $data['yongjin'] = $pinfo['yongjin'] ; 
            $data['create_time'] = time(); 
            $data['status'] = 0 ; 
            $rt = M('baobei')->add($data);
            if($rt){
                echo json_encode(array('status'=>1,'info'=>'报备申请提交成功')) ; 
                exit() ;
            }else{
                echo json_encode(array('status'=>0,'info'=>'操作失败')) ; 
                exit() ;
            }
        }
    }
}